//
//  TabItemDictionary.m
//  AutoCompiler_Script
//
//  Created by Vescky on 12-12-18.
//  Copyright (c) 2012年 Vescky. All rights reserved.
//

#import "TabItemDictionary.h"
#import "FileOperator.h"
#import "PathParser.h"
#import "RegexKitLite.h"

@implementation TabItemDictionary
@synthesize tabDictionary;

- (id)init {
    self = [super init];
    tabDictionary = [[NSMutableDictionary alloc] init];
//    [self searchForHomePage_2];
    [self serachForTab_3];
    [self searchForDir:@"More"];
    [self searchForDir:@"Home_Page"];
    return self;
}

#pragma mark - 搜索所有文件 V1.0
- (void)searchForHomePage {
    NSString *classFolder = [self getClassFolderPath];
    NSString *homePageFolder = [classFolder stringByAppendingPathComponent:@"Home_Page"];
    NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:homePageFolder] objectAtIndex:0];
    NSMutableDictionary *homePageDictionary = [[NSMutableDictionary alloc] init];
    
    for (int i = 0; i < [layoutFolderArray count]; i++) {
        NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",homePageFolder,[layoutFolderArray objectAtIndex:i]];
        [homePageDictionary setObject:[self ergodicLayoutFolder:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
    }
    [tabDictionary setObject:homePageDictionary forKey:@"Home_Page"];
    
}

- (void)serachForTab {
    NSString *classFolder = [self getClassFolderPath];
    NSString *tabFolder = [classFolder stringByAppendingPathComponent:@"Tab"];
    NSMutableArray *tabNameFolderArray = [[FileOperator ergodicFolder:tabFolder] objectAtIndex:0];
    
    for (int i = 0; i < [tabNameFolderArray count]; i++) {
        NSString *tabNameFolder = [NSString stringWithFormat:@"%@/%@",tabFolder,[tabNameFolderArray objectAtIndex:i]];
        NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:tabNameFolder] objectAtIndex:0];
        NSMutableDictionary *_tabDictionary = [[NSMutableDictionary alloc] init];
        
        for (int i = 0; i < [layoutFolderArray count]; i++) {
            NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",tabNameFolder,[layoutFolderArray objectAtIndex:i]];
            [_tabDictionary setObject:[self ergodicLayoutFolder:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
        }
        [tabDictionary setObject:_tabDictionary forKey:[tabNameFolderArray objectAtIndex:i]];
    }

}

- (NSMutableArray*)ergodicLayoutFolder:(NSString*)_layoutFolder {
    NSString *ControllerFolder = [NSString stringWithFormat:@"%@/Controller",_layoutFolder];
    NSString *ResourcesFolder1 = [NSString stringWithFormat:@"%@/Resources/Images/320*480",_layoutFolder];
    NSString *ResourcesFolder2 = [NSString stringWithFormat:@"%@/Resources/Images/640*960",_layoutFolder];
    NSString *ViewFolder = [NSString stringWithFormat:@"%@/View",_layoutFolder];
    NSMutableArray *layoutItems = [[NSMutableArray alloc] init];
    [layoutItems addObject:ControllerFolder];
    [layoutItems addObject:ResourcesFolder1];
    [layoutItems addObject:ResourcesFolder2];
    [layoutItems addObject:ViewFolder];
    
    NSMutableArray *fileListInLayout = [[NSMutableArray alloc] init];
    for (int j = 0; j < [layoutItems count]; j++) {
        
        NSMutableArray *itemSource = [[FileOperator ergodicFolder:[layoutItems objectAtIndex:j]] objectAtIndex:1];
        for (int k = 0; k < [itemSource count]; k++) {
            [fileListInLayout addObject:[itemSource objectAtIndex:k]];
        }
    }
    return fileListInLayout;
}


//_fileList:包含dictionary的数组，dictionary key- tabName,layout
- (NSMutableArray*)getConfigFromFileList:(NSMutableArray*)_fileList {
    if (!_fileList || [_fileList count] <= 0) {
        return nil;
    }
    
    for (int i = 0; i < [_fileList count]; i++) {
        NSDictionary *_dict = [_fileList objectAtIndex:i];
        NSString *_tabName = [_dict objectForKey:@"tabName"];
        NSString *layoutName = [_dict objectForKey:@"layout"];
        NSMutableDictionary *_dictFromTabDictionay = [tabDictionary objectForKey:_tabName];
        if (_dictFromTabDictionay && [_dictFromTabDictionay count] > 0) {
//            NSLog(@"%@",[_dictFromTabDictionay objectForKey:layoutName]);
            [_dictFromTabDictionay removeObjectForKey:layoutName];
            [tabDictionary setObject:_dictFromTabDictionay forKey:_tabName];
        }
    }
    
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    NSEnumerator *tabNameEnum = [tabDictionary keyEnumerator];
    for (NSString *tabNameKey in tabNameEnum) {
        if ([[tabNameKey lowercaseString] stringByMatching:@"lynx"]) {
            continue;
        }
        NSDictionary *_layoutDict = [tabDictionary objectForKey:tabNameKey];
        NSEnumerator *layoutEnum = [_layoutDict keyEnumerator];
        for (NSString *layoutKey in layoutEnum) {
            NSArray *_arr = [_layoutDict objectForKey:layoutKey];
            if (_arr && [_arr count]> 0) {
                for (int i = 0; i < [_arr count]; i++) {
                    [resultArray addObject:[_arr objectAtIndex:i]];
                }
            }
        }
    }
    
    return resultArray;
}

#pragma mark - 搜索所有文件 V2.0   调用递归算法
- (void)searchForHomePage_2 {
    NSString *classFolder = [self getClassFolderPath];
    NSString *homePageFolder = [classFolder stringByAppendingPathComponent:@"Home_Page"];
    
    NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:homePageFolder] objectAtIndex:0];
    layoutFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:layoutFolderArray];//删除系统的
    
    NSMutableDictionary *homePageDictionary = [[NSMutableDictionary alloc] init];
    
    for (int i = 0; i < [layoutFolderArray count]; i++) {
        NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",homePageFolder,[layoutFolderArray objectAtIndex:i]];
        [homePageDictionary setObject:[self ergodicLayoutFolder_2:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
    }
    [tabDictionary setObject:homePageDictionary forKey:@"Home_Page"];
}


- (void)serachForTab_2 {
    NSString *classFolder = [self getClassFolderPath];
    NSString *tabFolder = [classFolder stringByAppendingPathComponent:@"Tab"];
    NSMutableArray *tabNameFolderArray = [[FileOperator ergodicFolder:tabFolder] objectAtIndex:0];
    tabNameFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:tabNameFolderArray];//删除系统的
    
    for (int i = 0; i < [tabNameFolderArray count]; i++) {
        NSString *tabNameFolder = [NSString stringWithFormat:@"%@/%@",tabFolder,[tabNameFolderArray objectAtIndex:i]];
        NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:tabNameFolder] objectAtIndex:0];
        layoutFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:layoutFolderArray];//删除系统的
        NSMutableDictionary *_tabDictionary = [[NSMutableDictionary alloc] init];
        
        for (int i = 0; i < [layoutFolderArray count]; i++) {
            NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",tabNameFolder,[layoutFolderArray objectAtIndex:i]];
            [_tabDictionary setObject:[self ergodicLayoutFolder_2:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
        }
        [tabDictionary setObject:_tabDictionary forKey:[tabNameFolderArray objectAtIndex:i]];
    }
}

- (NSMutableArray*)ergodicLayoutFolder_2:(NSString*)_layoutFolder {
    FileOperator *fo = [FileOperator shareFileOperator];
    NSMutableArray *arr = [fo fileErgodicor:_layoutFolder];
    arr = [fo deleteSystemHiddenFileFromList:arr];
    arr = [fo deleteFileWithoutExtensionFromList:arr];
    return arr;
}

#pragma mark - 搜索所有文件 V3.0 过滤base目录
- (void)searchForHomePage_3 {
    NSString *classFolder = [self getClassFolderPath];
    NSString *homePageFolder = [classFolder stringByAppendingPathComponent:@"Home_Page"];
    
    NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:homePageFolder] objectAtIndex:0];
    layoutFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:layoutFolderArray];//删除系统的
    
    NSMutableDictionary *homePageDictionary = [[NSMutableDictionary alloc] init];
    
    for (int i = 0; i < [layoutFolderArray count]; i++) {
        NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",homePageFolder,[layoutFolderArray objectAtIndex:i]];
        [homePageDictionary setObject:[self ergodicLayoutFolder_2:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
    }
    [tabDictionary setObject:homePageDictionary forKey:@"Home_Page"];
}


- (void)serachForTab_3 {
    NSString *classFolder = [self getClassFolderPath];
    NSString *tabFolder = [classFolder stringByAppendingPathComponent:@"Tab"];
    NSMutableArray *tabNameFolderArray = [[FileOperator ergodicFolder:tabFolder] objectAtIndex:0];
    tabNameFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:tabNameFolderArray];//删除系统的
    //tabNameFolderArray = [self deleteBaseFolderFromArray:tabNameFolderArray];//去除base目录
    
    for (int i = 0; i < [tabNameFolderArray count]; i++) {
        NSString *tabNameFolder = [NSString stringWithFormat:@"%@/%@",tabFolder,[tabNameFolderArray objectAtIndex:i]];
        NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:tabNameFolder] objectAtIndex:0];
        layoutFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:layoutFolderArray];//删除系统的
        //layoutFolderArray = [self deleteBaseFolderFromArray:layoutFolderArray];//去除base目录
        NSMutableDictionary *_tabDictionary = [[NSMutableDictionary alloc] init];
        
        for (int i = 0; i < [layoutFolderArray count]; i++) {
            NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",tabNameFolder,[layoutFolderArray objectAtIndex:i]];
            [_tabDictionary setObject:[self ergodicLayoutFolder_2:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
        }
        [tabDictionary setObject:_tabDictionary forKey:[tabNameFolderArray objectAtIndex:i]];
    }
}
                          

- (NSMutableArray*)deleteBaseFolderFromArray:(NSMutableArray*)arr {
    NSMutableArray *arrResult = [[NSMutableArray alloc] init];
    for (int i = 0; i < [arr count]; i++) {
        NSString *str = [arr objectAtIndex:i];
        if (![[str lowercaseString] isEqualToString:@"base"]) {
            [arrResult addObject:str];
        }
    }
    return arrResult;
}
                          
//过滤保留tab
- (NSMutableArray*)tabFilter:(NSMutableArray*)arr {
    if (!arr) {
        return nil;
    }
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < arr.count; i++) {
        NSString *fName = [arr objectAtIndex:i];
        if (![[fName lowercaseString] stringByMatching:@"lynx"]) {
            [resultArr addObject:fName];
        }
    }
    return resultArr;
}
                          
                          
#pragma mark - 搜索所有文件 V4.0
- (void)searchForDir:(NSString*)dirName {
    NSString *classFolder = [self getClassFolderPath];
    NSString *goalDir = [classFolder stringByAppendingPathComponent:dirName];
    
    NSMutableArray *layoutFolderArray = [[FileOperator ergodicFolder:goalDir] objectAtIndex:0];
    layoutFolderArray = [[FileOperator shareFileOperator] deleteSystemHiddenFileFromList:layoutFolderArray];//删除系统的
    
    NSMutableDictionary *goalDict = [[NSMutableDictionary alloc] init];
    
    for (int i = 0; i < [layoutFolderArray count]; i++) {
        NSString *layoutFolder = [NSString stringWithFormat:@"%@/%@",goalDir,[layoutFolderArray objectAtIndex:i]];
        [goalDict setObject:[self ergodicLayoutFolder_2:layoutFolder] forKey:[layoutFolderArray objectAtIndex:i]];
    }
    [tabDictionary setObject:goalDict forKey:dirName];
}

#pragma mark - 其他方法
//返回特定tab的layout或者classes目录包含的datamodel文件数组
+ (NSMutableArray*)searchForXcmodelInTabName:(NSString*)tabName layoutName:(NSString*)layoutName {
    NSString *classFolder = [[TabItemDictionary alloc] getClassFolderPath];
    NSString *path = classFolder;
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    //遍历整个classes
    if (!tabName && !layoutName) {
        arr = [[FileOperator shareFileOperator] fileErgodicor:path withExtension:@"xcdatamodel"];
        return arr;
    }
    //遍历特定tab
    if ([tabName isEqualToString:@"Home_page"]) {
        path = [path stringByAppendingFormat:@"/%@/%@",tabName,layoutName];
    }
    else {
         path = [path stringByAppendingFormat:@"/Tab/%@/%@",tabName,layoutName];
    }
    arr = [[FileOperator shareFileOperator] fileErgodicor:path withExtension:@"xcdatamodel"];
    return arr;
}


+ (NSMutableArray*)searchForXcmodelInHomePageAndTabs {
    NSString *classFolder = [[TabItemDictionary alloc] getClassFolderPath];
    NSString *pathForTab = [classFolder stringByAppendingPathComponent:@"Tab"];
    NSString *pathForHomePage = [classFolder stringByAppendingPathComponent:@"Home_Page"];
    
    NSMutableArray *arr1 = [[NSMutableArray alloc] init];
    arr1 = [[FileOperator shareFileOperator] fileErgodicor:pathForTab withExtension:@"xcdatamodel"];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    arr = [[FileOperator shareFileOperator] fileErgodicor:pathForHomePage withExtension:@"xcdatamodel"];
    
    [arr addObjectsFromArray:arr1];
    return arr;
}

- (NSString*)getClassFolderPath {
    PathParser *pp = [PathParser sharePathParser];
    NSString *classFolder = [NSString stringWithFormat:@"%@/%@/Classes",pp.outIosProjectPath,pp.templateProjectName];
    return classFolder;
}

@end
